<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车间" prop="factoryCode">
        <el-select v-model="queryParams.factoryCode" placeholder="请选择车间">
          <el-option
            v-for="item in dict.type.bus_plant"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="计划号" prop="jhNumber">
        <el-input
          v-model="queryParams.jhNumber"
          placeholder="请输入计划号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="造型线号" prop="zxxNumber">
        <el-input
          v-model="queryParams.zxxNumber"
          placeholder="请输入造型线号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品种" prop="variety">
        <el-input
          v-model="queryParams.variety"
          placeholder="请输入品种"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格" prop="specification">
        <el-input
          v-model="queryParams.specification"
          placeholder="请输入规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="形式" prop="modality">
        <el-input
          v-model="queryParams.modality"
          placeholder="请输入形式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="模具编号" prop="mjNumber">
        <el-input
          v-model="queryParams.mjNumber"
          placeholder="请输入模具编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bus:shape:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bus:shape:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bus:shape:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bus:shape:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shapeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="业务主键ID" align="center" prop="id" />
      <el-table-column label="车间代号" align="center" prop="factoryCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_plant" :value="scope.row.factoryCode"/>
        </template>
      </el-table-column>
      <el-table-column label="计划号" align="center" prop="jhNumber" />
      <el-table-column label="造型线号" align="center" prop="zxxNumber" />
      <el-table-column label="品种" align="center" prop="variety" />
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="形式" align="center" prop="modality" />
      <el-table-column label="计划造箱" align="center" prop="jhzx" />
      <el-table-column label="实际造型箱数" align="center" prop="zxBoxNumber" />
      <el-table-column label="实际造型件数" align="center" prop="zxPieceNumber" />
      <el-table-column label="造型开始时间" align="center" prop="zxBeginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.zxBeginTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="造型结束时间" align="center" prop="zxEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.zxEndTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="造型停机时长" align="center" prop="zxTimes" />
      <el-table-column label="模具编号" align="center" prop="mjNumber" />
      <el-table-column label="清料位器时间" align="center" prop="qlwqTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.qlwqTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="浇注测温时间" align="center" prop="jzcwTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jzcwTime,'{y}-{m}-{d} {h}:{m}:{s}')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际浇注箱数" align="center" prop="jzBoxNumber" />
      <el-table-column label="浇注温度" align="center" prop="jzTemperature" />
      <el-table-column label="箱件数" align="center" prop="boxPieceNumber" />
      <el-table-column label="停机编码" align="center" prop="shutNum" />
      <el-table-column label="停机原因" align="center" prop="downCause" />
      <el-table-column label="暂停原因 " align="center" prop="poseReason" />
      <el-table-column label="终止原因" align="center" prop="delReason" />
      <el-table-column label="暂停时间" align="center" prop="poseTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.poseTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="恢复时间" align="center" prop="coverTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.coverTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="其他备注，造型分析备注原因" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bus:shape:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bus:shape:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改造型记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="车间" prop="factoryCode">
              <el-select v-model="form.factoryCode" placeholder="请选择车间">
                <el-option
                  v-for="item in dict.type.bus_plant"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="造型线号" prop="zxxNumber">
              <el-input v-model="form.zxxNumber" placeholder="请输入造型线号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="计划号" prop="jhNumber">
          <el-input v-model="form.jhNumber" placeholder="请输入计划号" />
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="品种" prop="variety">
              <el-input v-model="form.variety" placeholder="请输入品种" />
            </el-form-item>
            <el-form-item label="规格" prop="specification">
              <el-input v-model="form.specification" placeholder="请输入规格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="形式" prop="modality">
              <el-input v-model="form.modality" placeholder="请输入形式" />
            </el-form-item>

            <el-form-item label="模具编号" prop="mjNumber">
              <el-input v-model="form.mjNumber" placeholder="请输入模具编号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="计划造箱" prop="jhzx">
              <el-input-number v-model="form.jhzx" placeholder="请输入计划造箱" />
            </el-form-item>
            <el-form-item label="实际造型箱数" prop="zxBoxNumber">
              <el-input-number v-model="form.zxBoxNumber" placeholder="请输入实际造型箱数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际造型件数" prop="zxPieceNumber">
              <el-input-number v-model="form.zxPieceNumber" placeholder="请输入实际造型件数" />
            </el-form-item>
          </el-col>
        </el-row>



        <el-row>
          <el-col :span="12">
            <el-form-item label="造型开始时间" prop="zxBeginTime">
              <el-date-picker clearable
                              v-model="form.zxBeginTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择造型开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="造型结束时间" prop="zxEndTime">
              <el-date-picker clearable
                              v-model="form.zxEndTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择造型结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="造型停机时长" prop="zxTimes">
              <el-input-number v-model="form.zxTimes" placeholder="请输入造型停机时长" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="清料位器时间" prop="qlwqTime">
              <el-date-picker clearable
                              v-model="form.qlwqTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择清料位器时间">
              </el-date-picker>
            </el-form-item>

          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="实际浇注箱数" prop="jzBoxNumber">
              <el-input-number v-model="form.jzBoxNumber" placeholder="请输入实际浇注箱数" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="箱件数" prop="boxPieceNumber">
              <el-input-number v-model="form.boxPieceNumber" placeholder="请输入箱件数" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="浇注温度" prop="jzTemperature">
          <el-input v-model="form.jzTemperature" placeholder="请输入浇注温度" />
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="停机编码" prop="shutNum">
              <el-input v-model="form.shutNum" placeholder="请输入停机编码" />
            </el-form-item>
            <el-form-item label="停机原因" prop="downCause">
              <el-input v-model="form.downCause" placeholder="请输入停机原因" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="暂停原因 " prop="poseReason">
              <el-input v-model="form.poseReason" placeholder="请输入暂停原因 " />
            </el-form-item>
            <el-form-item label="终止原因" prop="delReason">
              <el-input v-model="form.delReason" placeholder="请输入终止原因" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="暂停时间" prop="poseTime">
              <el-date-picker clearable
                              v-model="form.poseTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择暂停时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="恢复时间" prop="coverTime">
              <el-date-picker clearable
                              v-model="form.coverTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择恢复时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="造型分析备注原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShape, getShape, delShape, addShape, updateShape } from "@/api/bus/shape";

export default {
  name: "Shape",
  dicts:['bus_plant'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 造型记录表格数据
      shapeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryCode: null,
        jhNumber: null,
        zxxNumber: null,
        variety: null,
        specification: null,
        modality: null,
        jhzx: null,
        zxBoxNumber: null,
        zxPieceNumber: null,
        zxBeginTime: null,
        zxEndTime: null,
        zxTimes: null,
        mjNumber: null,
        qlwqTime: null,
        jzcwTime: null,
        jzBoxNumber: null,
        jzTemperature: null,
        boxPieceNumber: null,
        shutNum: null,
        downCause: null,
        poseReason: null,
        delReason: null,
        poseTime: null,
        coverTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        factoryCode: [
          { required: true, message: '请选择车间', trigger: 'blur' }
        ],
        zxxNumber: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        variety: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        specification: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        modality: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        zxBoxNumber: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询造型记录列表 */
    getList() {
      this.loading = true;
      listShape(this.queryParams).then(response => {
        this.shapeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryCode: null,
        jhNumber: null,
        zxxNumber: null,
        variety: null,
        specification: null,
        modality: null,
        jhzx: null,
        zxBoxNumber: null,
        zxPieceNumber: null,
        zxBeginTime: null,
        zxEndTime: null,
        zxTimes: null,
        mjNumber: null,
        qlwqTime: null,
        jzcwTime: null,
        jzBoxNumber: null,
        jzTemperature: null,
        boxPieceNumber: null,
        shutNum: null,
        downCause: null,
        createTime: null,
        poseReason: null,
        delReason: null,
        poseTime: null,
        coverTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加造型记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getShape(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改造型记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShape(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShape(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除造型记录编号为"' + ids + '"的数据项？').then(function() {
        return delShape(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bus/shape/export', {
        ...this.queryParams
      }, `shape_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
